<?php

namespace app\admin\controller\system\config;

use app\common\controller\AdminController;
use app\services\system\config\SystemConfigService;
use app\services\system\config\SystemConfigTabService;
use think\App;


/**
 * 配置分类
 * Class SystemConfigTab
 * @package app\adminapi\controller\setting
 */
class SystemConfigTab extends AdminController
{
    private SystemConfigTabService $services;

    /**
     * 构造方法
     * SystemConfigTab constructor.
     *
     * @param App                    $app
     * @param SystemConfigTabService $services
     */
    public function __construct(App $app, SystemConfigTabService $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create($tabId = 0)
    {
        $this->assign('pid', $tabId);
        $this->assign('categoryList', $this->services->getSelectForm());
        return $this->fetch();
    }

    /**
     * Description: 添加分类
     *
     * @param null $id
     */
    public function save($id = null)
    {
        $data = $this->request->params([
            'eng_title',
            ['status', 1],
            'title',
            'icon',
            ['type', 0],
            ['sort', 0],
            ['pid', 0],
        ]);
        if (!$data['title']) {
            $this->error('请输入配置名称');
        }
        if (!$data['eng_title']) {
            $this->error('请输入分类英文名称');
        }
        if (is_null($id)) {
            $this->services->save($data);
        } else {
            $this->services->update($id, $data);
        }
        $this->success((is_null($id) ? '添加' : '修改') . '配置分类成功!');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     *
     * @return \think\Response
     */
    public function edit($id)
    {
        $this->assign('categoryList', $this->services->getSelectForm());
        $this->assign('row', $this->services->get($id));
        return $this->fetch();
    }

    /**
     * Description: 删除指定资源
     *
     * @param SystemConfigService $services
     * @param                     $id
     */
    public function delete(SystemConfigService $services, $id)
    {
        if ($services->count(['tab_id' => $id])) {
            $this->error('存在下级配置，无法删除！');
        }
        if (!$this->services->delete($id)) {
            $this->error('删除失败,请稍候再试!');
        }
        $this->success('删除成功!');
    }

    /**
     * 修改状态
     *
     * @param $id
     * @param $status
     *
     * @return mixed
     */
    public function set_status($id, $status)
    {
        if ($status == '' || $id == 0) {
            $this->fail('参数错误');
        }
        $this->services->update($id, ['status' => $status]);
        $this->success($status == 0 ? '隐藏成功' : '显示成功');
    }
}
